package org.ysu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ysu.entity.Family;
import org.ysu.service.FamilyService;
import org.ysu.vo.ResultVO;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 贫困户表 前端控制器
 * </p>
 *
 * @author LiuJunDa
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/family")
public class FamilyController {

    @Resource
    FamilyService familyService;

    @PostMapping("/add")
    public ResultVO<Boolean> add(Long fId, String pName, String pNumber, Long rId, Long vcId, String province, String city,String county ,String township ,String village) {
        return ResultVO.success(familyService.add(fId,pName,pNumber,rId,vcId,province,city,county,township,village));
    }

    @GetMapping("/check")
    public ResultVO<Boolean> check(String value,Long id,String field){
        return ResultVO.success(familyService.check(value,id,field));
    }

    @GetMapping("/list")
    public ResultVO<IPage<Family>> list(Integer pageNo,Integer pageSize,Long rId,String fId) {
        Long id;
        if (StringUtils.isBlank(fId)) {
            id = null;
        } else {
            id = Long.valueOf(fId);
        }
        return ResultVO.success(familyService.list(pageNo,pageSize,rId,id));
    }

    @GetMapping("/getById")
    public ResultVO<Family> getByIdWithHead(Long id) {
        return ResultVO.success(familyService.getByIdWithHead(id));
    }

    @PostMapping("/update")
    public ResultVO<Boolean> update(Long fid, String pname, String pnumber, Long rid, String province, String city,String county ,String township ,String village) {
        return ResultVO.success(familyService.update(fid,pname,pnumber,rid,province,city,county,township,village),"修改家庭信息成功");
    }

    @PostMapping("alert")
    public ResultVO<Boolean> alert(Long fid,Boolean alert) {
        return ResultVO.success(familyService.updateById(new Family(fid,alert)));
    }

}
